package cn.shyouchi.wuguanjia.android.ui.register;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.youchi.YouChiApi;
import cn.shyouchi.wuguanjia.android.model.SmsCodeMod;
import cn.shyouchi.wuguanjia.android.model.SmsVerifyMod;
import cn.shyouchi.wuguanjia.android.model.User;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.utils.CheckForAllUtils;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Eenie on 2016/10/13 at 10:08
 * Email: 472279981@qq.com
 * Des:
 */

public class RegisterPresenter implements RegisterContract.Presenter {
    RegisterContract.View mView;
    YouChiApi mApi;
    Context mContext;

    User mUser;

    public RegisterPresenter(Context context) {
        mContext = context;
        mApi = new YouChiApi();

    }

    @Override
    public void attachView(@NonNull RegisterContract.View view) {
        this.mView = view;
    }

    @Override
    public void detachView() {

    }

    @Override
    public void fetchSmsCode(String phone) {
        if (CheckForAllUtils.isMobileNO(phone)) {
            mApi.fetchSmsCode(phone)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ProgressSubscriber<SmsCodeMod>(mContext) {
                        @Override
                        public void onNext(SmsCodeMod mod) {
                            mView.sendComplete(mod);
                        }
                    });
        } else {
            mView.showToast(mContext.getText(R.string.please_type_correct_telephone).toString());
        }
    }

    @Override
    public void register(final int type, final String phone, String code, final String pwd, String repwd, boolean isApply, SmsCodeMod mod) {
        if (TextUtils.isEmpty(phone)) {
            mView.showToast(mContext.getText(R.string.please_type_telephone).toString());
            return;
        }
        if (!CheckForAllUtils.isMobileNO(phone)) {
            mView.showToast(mContext.getText(R.string.please_type_correct_telephone).toString());
            return;
        }
        if (TextUtils.isEmpty(code)) {
            mView.showToast(mContext.getText(R.string.please_type_sms_code).toString());
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            mView.showToast(mContext.getText(R.string.please_type_password).toString());
            return;
        }
        if (TextUtils.isEmpty(repwd)) {
            mView.showToast(mContext.getText(R.string.please_confirm_password).toString());
            return;
        }
        if (!pwd.equals(repwd)) {
            mView.showToast(mContext.getText(R.string.password_is_not_same).toString());
            return;
        }
        if (!isApply) {
            mView.showToast(mContext.getText(R.string.please_read_rule).toString());
            return;
        }


        if (mod != null && mod.getResultCode() == 0) {
            mApi.verifySmsCode(mod.getData(), code)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ProgressSubscriber<SmsVerifyMod>(mContext) {
                        @Override
                        public void onNext(SmsVerifyMod mod) {
                            if (mod.getResultCode() == 0) {
                                mUser = new User();
                                mUser.setUsername(phone);
                                mUser.setPassword(pwd);
                                switch (type) {
                                    case RegisterActivity.SECURITY_REGISTER:
                                        mView.jumpBaseInfo(mUser);
                                        break;
                                    case RegisterActivity.PROPERTY_REGISTER:
                                        break;
                                }
                            } else {
                                mView.showToast(mod.getResultMessage());
                            }
                        }
                    });
        } else {
            mView.showToast("请先验证手机号码");
        }




    }
}
